package com.byq.mm.smartlib.activity;

import com.byq.mm.abstractx.cluster.server.ClientEntry;
import com.byq.mm.abstractx.cluster.server.ClusterServer;
import com.byq.mm.smartlib.R;
import com.byq.mm.smartlib.tools.IconManager;

import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.Button;
import android.widget.CompoundButton;
import android.widget.ImageView;
import android.widget.ListView;
import android.widget.TextView;
import android.widget.ToggleButton;

import java.util.List;

public class ClusterServerActivity extends AbstractSmartActivity {
	ClusterServer clusterServer;
	Button back;

	@Override
	public void onCreate(Bundle icicle) {
		super.onCreate(icicle);
		String deviceId = getIntent().getExtras().getString("deviceId");
		String clusterId = getIntent().getExtras().getString("clusterId");
		clusterServer = application.getAndroidSmartEngine().getCoreEngine().getServerDeviceManager().getDeviceServer(
				deviceId).getClusterServer(clusterId);
		this.setContentView(R.layout.cluster_server);
		ListView view = (ListView) this.findViewById(R.id.clients);
		view.setAdapter(new ClusterServerAdapter());
		
		ImageView icon = (ImageView) this.findViewById(R.id.clusterIcon);
		TextView name = (TextView) this.findViewById(R.id.clusterName);
		back = (Button) this.findViewById(R.id.back_cluster_server);

		icon.setImageDrawable(IconManager.getIconDrawable(this, clusterId));
		int rid = getResources().getIdentifier(clusterId,  "string",getPackageName());
		name.setText(rid);

		back.setOnClickListener(new View.OnClickListener(){

			@Override
			public void onClick(View view) {
				ClusterServerActivity.this.finish();
			}
		});
	}

	class ClusterServerAdapter extends BaseAdapter {
		List<ClientEntry> entrys;

		public ClusterServerAdapter() {
			super();
			this.entrys = clusterServer.getAccessController().getClientEntrys();
		}

		@Override
		public int getCount() {
			// TODO Auto-generated method stub
			return entrys.size();
		}

		@Override
		public Object getItem(int position) {
			return entrys.get(position);
		}

		@Override
		public long getItemId(int position) {
			// TODO Auto-generated method stub
			return 0;
		}

		@Override
		public View getView(int position, View view, ViewGroup parent) {
			if (view == null){
				view = LayoutInflater.from(ClusterServerActivity.this).inflate(
						R.layout.list_item_icon_icon_text_toggle, null);
			final ClientEntry elem = entrys.get(position);

			ImageView icon = (ImageView) view.findViewById(R.id.icon);
			icon.setVisibility(View.GONE);
			TextView text = (TextView) view.findViewById(R.id.text);

			text.setText(elem.clientTransport);
			text.setPadding(10, 10, 0, 10);

			ToggleButton toggle = (ToggleButton) view.findViewById(R.id.toggle);

			toggle.setChecked(elem.permitted);
			toggle
					.setOnCheckedChangeListener(new ToggleButton.OnCheckedChangeListener() {

						@Override
						public void onCheckedChanged(CompoundButton buttonView,
								boolean isChecked) {
							clusterServer.getAccessController().toggle(
									elem.clientTransport);

						}

					});

			ImageView devic = (ImageView) view.findViewById(R.id.entity_icon);
			devic.setImageResource(R.drawable.presence_online);
			
			
			ImageView arrow = (ImageView) view.findViewById(R.id.arrow);
			arrow.setVisibility(View.GONE);
			}

			return view;
		}

	}
}
